<?php
/**
 * @Copyright (c) 2011, 新浪网运营部-网络应用开发部
 * All rights reserved.
 * HTTP基类
 * @author          wangxin <wangxin3@staff.sina.com.cn>
 * @time            2011/3/2 11:48
 * @version         Id: 0.9
*/

class BaseModelHttp {

    private function __construct(){
        return;
    }
    
    static public function post($url, $data, array $header = array()){
        if(!function_exists('curl_init')) {
            BaseModelMessage::showError('服务器没有安装curl扩展！');
        }
        if(!is_array($data)) {
            throw new Exception('data参数不是一个数组！');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        $data = curl_exec($ch);
        list($header, $data) = explode("\n\n", $data, 2);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($http_code == 301 || $http_code == 302) {
            $matches = array();
            preg_match('/Location:(.*?)\n/', $header, $matches);
            $url = trim(array_pop($matches));
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, false);
            $data = curl_exec($ch);
        }

        if($data == false) {
            curl_close($ch);
        }
        @curl_close($ch);
        return $data;
    }

    static public function get($url, array $header = array()){
        if(!function_exists('curl_init')) {
            BaseModelMessage::showError('服务器没有安装curl扩展！');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, true);
        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        $data = curl_exec($ch);
        list($header, $data) = explode("\n\n", $data, 2);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($http_code == 301 || $http_code == 302) {
            $matches = array();
            preg_match('/Location:(.*?)\n/', $header, $matches);
            $url = trim(array_pop($matches));
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, false);
            $data = curl_exec($ch);
        }

        if($data == false) {
            curl_close($ch);
        }
        @curl_close($ch);
        return $data;
    }

    /**
     *   * 发送请求不等待接收
     *   * by wangxin3
     *   * @param $url string 发送请求url
     *   * @return void
     */
    public function sendRequest($url, $host = '') {
        $urlArr = parse_url($url);
        $fp = @fsockopen($urlArr['host'], 80, $errno, $errstr, 1);
        if ($fp) {
            stream_set_timeout($fp,1);         
            $out = "GET {$urlArr['path']}?{$urlArr['query']} HTTP/1.1\r\n";
            if (!empty($host)) {
                $out .= "Host: {$host}\r\n";
            } else {
                $out .= "Host: {$urlArr['host']}\r\n";
            }
            $out .= "Connection: Close\r\n\r\n";
            fwrite($fp, $out);
            fclose ($fp);
        }
    }

}
?>